package com.easyJavaDemo.service.impl;

import com.easyJavaDemo.entity.po.Goods;
import com.easyJavaDemo.entity.query.GoodsQuery;
import com.easyJavaDemo.entity.query.SimplePage;
import com.easyJavaDemo.enums.PageSize;
import com.easyJavaDemo.entity.vo.PaginationResultVO;
import com.easyJavaDemo.service.GoodsService;
import com.easyJavaDemo.mapper.GoodsMapper;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品表Service接口
 */
@Service
public class GoodsServiceImpl implements GoodsService{

	/**
	 * GoodsMapper对象
	 */
	@Autowired
	private GoodsMapper goodsMapper;


	/**
	 * 新增
	 */
	public int add(Goods goods){
		return goodsMapper.insert(goods);
	}

	/**
	 * 新增或修改
	 */
	public int addOrUpdate(Goods goods){
		return goodsMapper.insertOrUpdate(goods);
	}

	/**
	 * 批量新增
	 */
	public long addBatch(List<Goods> list){
		return goodsMapper.insertBatch(list);
	}

	/**
	 * 批量新增或修改
	 */
	public long addOrUpdateBatch(List<Goods> list){
		return goodsMapper.insertOrUpdateBatch(list);
	}

	/**
	 * 根据参数获取集合
	 */
	public List<Goods> getList(GoodsQuery goodsQuery){
		return goodsMapper.selectList(goodsQuery);
	}

	/**
	 * 根据参数查询数量
	 */
	public long getCount(GoodsQuery goodsQuery){
		return goodsMapper.selectCount(goodsQuery);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<Goods> getPageList(GoodsQuery goodsQuery){
		long count = this.getCount(goodsQuery);
		int pageSize = goodsQuery.getPageSize()==null ? PageSize.SIZE10.getSize() : goodsQuery.getPageSize();
		SimplePage page = new SimplePage(goodsQuery.getPageNo(), count, pageSize);
		goodsQuery.setSimplePage(page);
		List<Goods> list = this.getList(goodsQuery);
		PaginationResultVO<Goods> result = new PaginationResultVO<>(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 根据Id查询
	 */
	public Goods getById(Integer id){
		return goodsMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	public int updateById(Goods goods, Integer id){
		return goodsMapper.updateById(goods, id);
	}

	/**
	 * 根据Id删除
	 */
	public int deleteById(Integer id){
		return goodsMapper.deleteById(id);
	}

	/**
	 * 根据NameAndPrice查询
	 */
	public Goods getByNameAndPrice(String name, Integer price){
		return goodsMapper.selectByNameAndPrice(name, price);
	}

	/**
	 * 根据NameAndPrice修改
	 */
	public int updateByNameAndPrice(Goods goods, String name, Integer price){
		return goodsMapper.updateByNameAndPrice(goods, name, price);
	}

	/**
	 * 根据NameAndPrice删除
	 */
	public int deleteByNameAndPrice(String name, Integer price){
		return goodsMapper.deleteByNameAndPrice(name, price);
	}

}
